﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KLS.PPDS.ApplicationService.Manager;
using KLS.PPDS.Core.Entity;
using KLS.PPDS.Utility;

namespace KLS.PPDS.Web.Models
{
    /// <summary>
    /// viewModel for story board will hold data for storyboard view
    /// </summary>
    public class StoryboardViewModel
    {


        /// <summary>
        /// publication for which storyboard needs to be generated
        /// </summary>

        public Publication Publication { get; set; }

        /// <summary>
        /// All Articel for this Publication
        /// </summary>
        public List<Article> ArticleList { get; set; }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageNumer"></param>
        /// <returns></returns>
        public List<Article> GetArticleByPageNumber(int pageNumer)
        {
            //var article =
            //    ArticleList.FindAll(x => x.StartPage <= pageNumer && pageNumer <= (x.StartPage + (x.ExtentValue1-1))).FirstOrDefault();

            var article = ArticleManager.GetArticleByPageNo(Publication.Id, pageNumer);



            return article;
        }

        /// <summary>
        /// returns the CSS class depending on page index
        /// </summary>
        /// <param name="index"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public string PageCss(int index, int page)
        {
            if (page<1)
            {
                return "";
            }
            if (index == 1)
            {
                return " firstPage";
            }
            if(index== page)
            {
                return " lastPage";
            }
            if (IsOdd(index))
            {
                return "oddPage";
            }
            else
            {
                return "evenPage";
            }


            

        }


        public string DivOpen(int index, int page)
        {
            if (page < 1)
            {
                return "";
            }
            if (index == 1)
            {
                return "<div class=\"pageGroup\">"; // first page
            }
            if (index == page)
            {
                return "<div class=\"pageGroup lastPageGroup\">"; // last page
            }
            if (!IsOdd(index))
            {
            
                return  "<div class=\"pageGroup\">"; // even page
            }

            return "";

        }


        public string DivClose(int index, int page)
        {
            if (page < 1)
            {
                return "";
            }
            if (index == 1)
            {
                return "</div>"; // first Page
            }
            if (index == page-1) //page before last page
            {
                if (!IsOdd(index))
                {
                  return "</div>";
                }
                
            }
            if (index == page)
            {
                return "</div>"; // lastpage
            }
            if (IsOdd(index))
            {
                return "</div>";
            }

            return "";

        }

        /// <summary>
        /// ArticleCss
        /// </summary>
        /// <param name="index"></param>
        /// <param name="article"></param>
        /// <returns></returns>
        public string ArticleCss(int index,Article article)
        {
            if (article.IsReadyForGraphic)
            {
                return "green" + GetArticelHeightCSS(index,article);
            }

            if (article.IsReadyForEditor)
            {
                return "blue" + GetArticelHeightCSS(index, article);
            }
            if (!article.IsReadyForEditor)
            {
                return "red" + GetArticelHeightCSS(index, article);
            }


            return "";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="articleContent"></param>
        /// <returns></returns>
        public string ArticleContentCss(ArticleContent articleContent)
        {
            const string defaultValue = " red";
            if (articleContent.IsReadyForGraphic)
            {
                return " green" ;
            }


            if (articleContent.IsReadyForEditor)
            {
                return " blue";
            }


            return defaultValue;
        }



        private string GetArticelHeightCSS(int currentPage, Article article)
        {
            var css = "";
            //Startpage = current page and endpage also = current page 
            if (Math.Truncate(article.StartPage.GetValueOrDefault(0.0)) == currentPage && Math.Truncate(article.EndPage.GetValueOrDefault(0.0)) == currentPage && CommonTask.GetFraction(article.ExtentValue2.GetValueOrDefault(0.0)) > 0)
            {
                css = GetArticleCssByLength(CommonTask.GetFraction(article.ExtentValue2.GetValueOrDefault(0.0)));
                return css;
            }

            // startpage = currentPage but end page not equal currentpage
            if (Math.Truncate(article.StartPage.GetValueOrDefault(0.0)) == currentPage && Math.Truncate(article.EndPage.GetValueOrDefault(0.0)) != currentPage && CommonTask.GetFraction(article.StartPage.GetValueOrDefault(0.0)) > 0)
            {
                if (CommonTask.GetFraction(article.EndPage.GetValueOrDefault(0.0)) > 0)
                {
                    var length = Math.Truncate(article.EndPage.GetValueOrDefault(0.0)) -
                                 article.StartPage.GetValueOrDefault(0.0);

                    css = GetArticleCssByLength(CommonTask.GetFraction(length));  
                }
                else
                {
                    css = GetArticleCssByLength(CommonTask.GetFraction(article.ExtentValue2.GetValueOrDefault(0.0)));
                }

                return css;
            }

            //endpage equal current page but startpage not equal currentPage
            if (Math.Truncate(article.EndPage.GetValueOrDefault(0.0)) == currentPage && Math.Truncate(article.StartPage.GetValueOrDefault(0.0)) != currentPage && CommonTask.GetFraction(article.EndPage.GetValueOrDefault(0.0)) > 0)
            {
                css = GetArticleCssByLength(CommonTask.GetFraction(article.EndPage.GetValueOrDefault(0.0)));
                return css;
            }


            return css;
        }

        //private string GetArticelHeightCSS(int currentPage, Article article)
        //{
        //    if (GetEndpage(article.EndPage.GetValueOrDefault(0.0)) == currentPage)
        //    {
        //        var css = GetArticleCssByLength(CommonTask.GetFraction(article.ExtentValue2.GetValueOrDefault(0.0)));
        //        return css;
        //    }

        //    if (GetStartPage(article.StartPage.GetValueOrDefault(0.0)) == currentPage)
        //    {
        //        var css = GetArticleCssByLength(CommonTask.GetFraction(article.StartPage.GetValueOrDefault(0.0)));
        //        return css;
        //    }

        //    return "";
        //}

        private string GetArticleCssByLength(double  lenght)
        {
            if (lenght == 0.75)
            {
                return " ThreeQuarter";
            }

            if (lenght == 0.5)
            {
                return " half";
            }


            if (lenght == 0.25)
            {
                return " quarter";
            }



            return "";
        }





        /// <summary>
        /// return All the Standby Article for this publication
        /// </summary>
        public List<Article> StandByArticleList { get; set; }

        /// <summary>
        /// Check the given number 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool IsOdd(int value)
        {
            return value % 2 != 0;
        }


        /// <summary>
        /// add articel for empty StandBy
        /// </summary>
        
        public void AddArticleForEmtpyStandBy()
        {
            if (ArticleList.Count>0)
            {
                while (StandByArticleList.Count < 2)
                {
                    StandByArticleList.Add(new Article());
                }                 
            }

            
        }

        public bool HasLayoutPdf { get; set; }
      

    }
}